clear all
cap close
set more off

cd ".\Data_Program"

use county,clear

* merge with division data
merge m:1 county using Econdata\countytodivision
keep if _merge==3 
drop _merge

* merge with neighbor data
merge 1:1 quarter county using distance_weighted.dta
keep if _merge==3
drop _merge

* merge with Mintel data (proprietary)
merge 1:1 county quarter using Proprietary\mintel
drop if _merge==2 
drop _merge 
replace finad=0 if finad==.
replace nonfinad=0 if nonfinad==.
replace ad=0 if ad==.
gen finadshare = finad/ad

* merge with own economic data

    * CoreLogic house price growth (proprietary)
merge 1:1 quarter county using Proprietary\hpi_county_quarter_exp
drop if _merge==2
drop _merge
drop qhpi
replace hpgr=hpgr4

    * Census Buearu population growth
gen year=yofd(dofq(quarter))	
merge m:1 year county using Econdata\pop_county
drop if _merge==2
drop _merge
drop fracm frach

    * QCEW employment growth
merge m:1 year county using Econdata\emp_county_year
drop if _merge==2
drop _merge

    * CCP subprime share
merge 1:1 quarter county using Proprietary\subprime_county_quarter
drop if _merge==2  
drop _merge 

     * Census education level in 2010
merge m:1 county using Econdata\edu
drop if _merge==2  
drop _merge 

* demographic X year controls
tab year, gen(yy)
foreach i of numlist 1/10{
    gen baboveyy`i' = babove*yy`i'
	gen babove2yy`i' = babove2*yy`i'
	gen fracbyy`i' = fracb*yy`i'
	gen fracyyy`i' = fracy*yy`i'
}


* IRS migration flows
merge m:1 year county using Econdata\migration
drop if _merge==2  
drop _merge


* Regressions
xtset county quarter

* weights
sort county quarter
by  county: egen w1 = mean(tot)
by  county: egen w2 = mean(pop)
gen w=w1

  * outcome var
gen dfinshare =finshare-l4.finshare  
gen dfinshare2=finshare2-l4.finshare2 

gen dfinad = (log(finad+1)-log(l4.finad+1))/100
gen dfinad2 = (log(finad2+1)-log(l4.finad2+1))/100

global control2 hpgr popgr empgr fracyyy* fracbyy* subprime baboveyy* dfinad hpgr2 popgr2 empgr2 fracy2 fracb2 subprime2 babove2yy* dfinad2 linflow loutflow

save beforereg_distance,replace


*===============================================================================
* Table 1. Col 4
*===============================================================================

use beforereg_distance,clear

reghdfe dfinshare dfinshare2 $control2 [aw=w],a(county division#quarter) cluster(county)

